﻿using Microsoft.EntityFrameworkCore;
using ReportingSystem.IRepository;
using ReportingSystem.Model;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace ReportingSystem.Repository
{
    public class UserRepository : IUserRepository
    {
        private readonly AppDbContext _context;

        public UserRepository(AppDbContext context)
        {
            _context = context;
        }

        public void Create(User entity)
        {
            _context.Users.Add(entity);
            _context.SaveChanges();
        }

        public User Get(string str)
        {
            return _context.Users.Where(b => b.account == str).FirstOrDefault();
        }

        public User Get(Func<User, bool> wherelambda)
        {
            return _context.Users.Include(o => o.Repairss).Where<User>(wherelambda).FirstOrDefault();
        }

        public List<User> GetAll()
        {
            return _context.Users.Include(o => o.Repairss).ToList();
        }

        //查询
        public virtual List<User> GetAll(Func<User, bool> wherelambda)
        {
            return _context.Users.Include(o => o.Repairss).Where<User>(wherelambda).ToList();
        }

        public List<User> GetAll(dynamic user)
        {
            List<User> data = new List<User>();
            try
            {
                string userName = user.userName.Value;
                if (!string.IsNullOrEmpty(userName))
                {
                    data = _context.Users.Where(o => o.userName == userName).ToList();
                }
            }
            catch {
                try
                {
                    var userAge = user.userAge.ToString();
                    int age = int.Parse(userAge);
                    data = _context.Users.Where(o => o.userAge == age).ToList();
                }
                catch {
                    try
                    {
                        var userSex = user.userSex.ToString();
                        int sex = int.Parse(userSex);
                        data = _context.Users.Where(o => o.userSex == sex).ToList();
                    }
                    catch {
                        try
                        {
                            string userTelphone = user.userTelphone.Value;
                            if (!string.IsNullOrEmpty(userTelphone))
                            {
                                data = _context.Users.Where(o => o.userTelphone == userTelphone).ToList();
                            }
                        }
                        catch {
                            try
                            {
                                var userRole = user.role.ToString();
                                int role = int.Parse(userRole);
                                data = _context.Users.Where(o => o.role == role).ToList();
                            }
                            catch {
                                try
                                {
                                    string account = user.account.Value;
                                    if (!string.IsNullOrEmpty(account))
                                    {
                                        data = _context.Users.Where(o => o.account == account).ToList();
                                    }
                                }
                                catch {
                                    try
                                    {
                                        var userState = user.state.ToString();
                                        int state = int.Parse(userState);
                                        data = _context.Users.Where(o => o.state == state).ToList();
                                    }
                                    catch { }
                                }
                                try
                                {
                                    var userState = user.state.ToString();
                                    int state = int.Parse(userState);
                                    data = data.Where(o => o.state == state).ToList();
                                }
                                catch { }
                            }
                            try
                            {
                                string account = user.account.Value;
                                if (!string.IsNullOrEmpty(account))
                                {
                                    data = data.Where(o => o.account == account).ToList();
                                }
                            }
                            catch { }
                            try
                            {
                                var userState = user.state.ToString();
                                int state = int.Parse(userState);
                                data = data.Where(o => o.state == state).ToList();
                            }
                            catch { }
                        }
                        try
                        {
                            var userRole = user.role.ToString();
                            int role = int.Parse(userRole);
                            data = data.Where(o => o.role == role).ToList();
                        }
                        catch { }
                        try
                        {
                            string account = user.account.Value;
                            if (!string.IsNullOrEmpty(account))
                            {
                                data = data.Where(o => o.account == account).ToList();
                            }
                        }
                        catch { }
                        try
                        {
                            var userState = user.state.ToString();
                            int state = int.Parse(userState);
                            data = data.Where(o => o.state == state).ToList();
                        }
                        catch { }
                    }
                    try
                    {
                        string userTelphone = user.userTelphone.Value;
                        if (!string.IsNullOrEmpty(userTelphone))
                        {
                            data = data.Where(o => o.userTelphone == userTelphone).ToList();
                        }
                    }
                    catch { }
                    try
                    {
                        var userRole = user.role.ToString();
                        int role = int.Parse(userRole);
                        data = data.Where(o => o.role == role).ToList();
                    }
                    catch { }
                    try
                    {
                        string account = user.account.Value;
                        if (!string.IsNullOrEmpty(account))
                        {
                            data = data.Where(o => o.account == account).ToList();
                        }
                    }
                    catch { }
                    try
                    {
                        var userState = user.state.ToString();
                        int state = int.Parse(userState);
                        data = data.Where(o => o.state == state).ToList();
                    }
                    catch { }
                }
                try
                {
                    var userSex = user.userSex.ToString();
                    int sex = int.Parse(userSex);
                    data = data.Where(o => o.userSex == sex).ToList();
                }
                catch { }
                try
                {
                    string userTelphone = user.userTelphone.Value;
                    if (!string.IsNullOrEmpty(userTelphone))
                    {
                        data = data.Where(o => o.userTelphone == userTelphone).ToList();
                    }
                }
                catch { }
                try
                {
                    var userRole = user.role.ToString();
                    int role = int.Parse(userRole);
                    data = data.Where(o => o.role == role).ToList();
                }
                catch { }
                try
                {
                    string account = user.account.Value;
                    if (!string.IsNullOrEmpty(account))
                    {
                        data = data.Where(o => o.account == account).ToList();
                    }
                }
                catch { }
                try
                {
                    var userState = user.state.ToString();
                    int state = int.Parse(userState);
                    data = data.Where(o => o.state == state).ToList();
                }
                catch { }
            }
            try
            {
                var userAge = user.userAge.ToString();
                int age = int.Parse(userAge);
                data = data.Where(o => o.userAge == age).ToList();
            }
            catch { }
            try
            {
                var userSex = user.userSex.ToString();
                int sex = int.Parse(userSex);
                data = data.Where(o => o.userSex == sex).ToList();
            }
            catch { }
            try
            {
                string userTelphone = user.userTelphone.Value;
                if (!string.IsNullOrEmpty(userTelphone))
                {
                    data = data.Where(o => o.userTelphone == userTelphone).ToList();
                }
            }
            catch { }
            try
            {
                var userRole = user.role.ToString();
                int role = int.Parse(userRole);
                data = data.Where(o => o.role == role).ToList();
            }
            catch { }
            try
            {
                string account = user.account.Value;
                if (!string.IsNullOrEmpty(account))
                {
                    data = data.Where(o => o.account == account).ToList();
                }
            }
            catch { }
            try
            {
                var userState = user.state.ToString();
                int state = int.Parse(userState);
                data = data.Where(o => o.state == state).ToList();
            }
            catch { }
            return data;
        }

        public void Update(User user)
        {
            _context.Users.Update(user);
            _context.SaveChanges();
        }
    }
}
